/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package job;


import java.util.Properties;

import utils.PropertyUtil;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;

public class Producer extends Thread{
  private final kafka.javaapi.producer.Producer<String, String> producer;
  private final Topic topic;
  private final Properties props = new Properties();
  
  /** Kafka消息主题. */
  public enum Topic {
		Station,
		SchedulePrice,
		Ticket,
		Pricedel,
		Vehicle,
		Departpassenger,
		Departpassengerdel,
		ErrorTicket,
		SelfSaleTicket,
		UpdateExchangefee,
		Ticketsources,
		Stationstaff,
		Outletsinfo,
		Stationbehindschedule,
		StationSends,
		PayRecords,
		FullReturnSchedules,
		LossTickets,
		Stationinsurance,
		Netcentreorg,
		StationViewgps,
		QHStationData,
		insurance,
		ScheduleCancel,
		StationWebTicket,
		SyncDelay,
		Vehicle_gansutest,
		Departpassenger_gansutest,
		Departpassengerdel_gansutest,
		ErrorTicket_gansutest,
		busTopic;
	  
  }
  public Producer(Topic topic){
	  String kafkaUrl = PropertyUtil.getProperty("kafka.url")+":"+PropertyUtil.getProperty("kafka.port");
	  props.put("serializer.class", "kafka.serializer.StringEncoder");//
	  props.put("metadata.broker.list", kafkaUrl);
	  props.put("request.required.acks", "1");//kafka会发送确认收到信息
	  props.put("message.send.max.retries", "5");//收到 error ack 重发次数
	  props.put("producer.type", "async");//同步发送
	  props.put("batch.num.messages", "2");//消息在producer缓存的条数 producer.type=async有效
	  props.put("queue.buffering.max.ms","5000");//async模式下消息缓存的时间,跟batch.num.messages不冲突，哪个先达到条件哪个先触发
	  props.put("compression.codec", "gzip");//消息压缩格式
    // Use random partitioner. Don't need the key type. Just set it to Integer.
    // The message is of type String.
    producer = new kafka.javaapi.producer.Producer<String, String>(new ProducerConfig(props));
    this.topic = topic;
  }
  
  public void run() {
    int messageNo = 1;
    while(messageNo < 100){
      String messageStr = new String("Message_" + messageNo);
      producer.send(new KeyedMessage<String, String>(topic.name(), messageStr,messageStr));
      messageNo++;
      try {
		Thread.sleep(1000);
      } catch (InterruptedException e) {
		e.printStackTrace();
      }
    }
    producer.close();
  }

}
